<style>
    #user-update {
        padding: 20px 25px 25px 0;
    }
</style>
<div class="layui-fluid" id="user-update">
    <form class="layui-form" action="" lay-filter="quest-add">

        <div class="layui-form-item">
            <label class="layui-form-label">科室：</label>
            <div class="layui-input-inline">
                <select name="deptId" lay-filter="deptId" id="deptId">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">标题：</label>
            <div class="layui-input-block">
                <input type="text" name="title" minlength="4"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选项1：</label>
            <div class="layui-input-block">
                <input type="text" name="awan1" minlength="4"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选项2：</label>
            <div class="layui-input-block">
                <input type="text" name="awan2" minlength="4"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选项3：</label>
            <div class="layui-input-block">
                <input type="text" name="awan3" minlength="4"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" align="center">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit
                    lay-filter="quest-add-submit" id="submit">
                <i style="display: none; margin-left: 30px"
                   class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                完成编辑
            </button>
        </div>

    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'xmSelect', 'validate'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            deptId,
            deptName,
            form = layui.form;

        form.render();

        febs.post(ctx + 'web/get/deptlist', null, function (data) {
            $("#deptId").empty();
            $("#deptId").append(new Option("请选择科室", ""));
            $.each(data.data, function (index, item) {
                $("#deptId").append(new Option(item.dept_NAME, item.id));
            });
            layui.form.render("select");
        });

        form.on('select(deptId)', function (data) {
            deptId = new Number(data.value)
            deptName = this.innerText
            console.log(deptId)
        });

        form.on('submit(quest-add-submit)', function (data) {
            if (!data.field.deptId) {
                febs.alert.warn('请选择科室');
                return false;
            }
            if (!data.field.title) {
                febs.alert.warn('请输入标题');
                return false;
            }
            if (!data.field.awan1) {
                febs.alert.warn('请输入选项-1');
                return false;
            }
            if (!data.field.awan2) {
                febs.alert.warn('请输入选项-2');
                return false;
            }
            if (!data.field.awan3) {
                febs.alert.warn('请输入选项-3');
                return false;
            }
            let loading = $(this).find('.layui-icon');
            if (loading.is(":visible")) return;
            loading.show();

            febs.post(ctx + 'web/add/questionnaire', {
                deptId: data.field.deptId,
                deptName: deptName,
                title: data.field.title,
                awan1: data.field.awan1,
                awan2: data.field.awan2,
                awan3: data.field.awan3,
            }, function (data) {
                if (data.code === 200) {
                    layer.closeAll();
                }
            });
            return false;
        });
    });
</script>